home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Snippets / Development Tools & Languages / DTSCPlusLibrary / Sources / Process.cp < prev    next >
Encoding:
Text File  |  1993-01-14  |  6.1 KB  |  259 lines  |  [TEXT/MPS ]

  1. /* _________________________________________________________________________________________________________ //
  2.   Copyright Â© 1992 Apple Computer, Inc. All rights reserved.
  3.   Macintosh Developer Technical Support.C++ Macintosh Toolbox Framework.
  4.   Originator: Kent Sandvik
  5.   Date: Wednesday, June 10, 1992 22:37:30
  6.   Revision comments are at the end of this file.
  7.   ---
  8.   TProcess is a Process Manager class.
  9.   Process.cp contains the class body information for the TProcess class member functions.
  10.   _________________________________________________________________________________________________________ */
  11.  
  12. // Include files
  13. #ifndef _PROCESS_
  14. #include "Process.h"
  15. #endif
  16.  
  17.  
  18. // _________________________________________________________________________________________________________ //
  19. // TProcess class member function implementations
  20.  
  21. //    CONSTRUCTORS & DESTRUCTORS
  22. #pragma segment Process                       
  23. TProcess::TProcess(ProcessSerialNumber theNum)
  24. // Default constructor.
  25. {
  26.     fFirstTime = true;                            // signal that we are inside constructor
  27.     fLast = false;
  28.     fProcessID.highLongOfPSN = theNum.highLongOfPSN;
  29.     fProcessID.lowLongOfPSN = theNum.lowLongOfPSN;
  30.  
  31.     fError = ::GetCurrentProcess(&fMyProcessID);// always get our own PSN
  32.     VASSERT(fError == noErr, ("pProblems with GetCurrentProcess() = %d", fError));
  33.  
  34.     fState = this->IProcess();                    // initialize the object
  35.  
  36.     fFirstPSN = fProcessID;                        // define the first one
  37.     fFirstTime = false;                            // signal this time is over
  38. }
  39.  
  40.  
  41. #pragma segment Process
  42. TProcess::~TProcess()
  43. // Destructor, we are not doing anything inside this one just now.
  44. {
  45. }
  46.  
  47.  
  48. //    INITIATION ROUTINES
  49. #pragma segment Process
  50. Boolean TProcess::IProcess()
  51. // We are using a special IProcess member function for initializing class fields to
  52. // known values.
  53. {
  54.     // get our own PSN number
  55.     if (fFirstTime)                                // our first PSN (our own)
  56.     {
  57.         fError = ::GetNextProcess(&fProcessID);
  58.         VASSERT(fError == noErr, ("Problems with GetNextProcess() = %d", fError));
  59.  
  60.         if (fError != noErr)
  61.             goto IProcessFalse;
  62.  
  63.         goto IProcessOK;
  64.     }
  65.     else
  66.     {
  67.         fError = ::GetNextProcess(&fProcessID);    // fetch other PSNs
  68.  
  69.         if (fError != noErr)
  70.         {
  71.             fLast = true;
  72.             this->First();
  73.             goto IProcessFalse;
  74.         }
  75.         else
  76.             goto IProcessOK;
  77.     }
  78. IProcessFalse:return false;
  79. IProcessOK:return true;
  80. }
  81.  
  82.  
  83. //     MAIN INTERFACE
  84. #pragma segment Process
  85. Boolean TProcess::KillApplication(ProcessSerialNumber* thePSN)
  86. // quit the application which is defined by the PSN
  87. {
  88.     AEAddressDesc target;
  89.     AppleEvent theAE,  theAEReply;
  90.  
  91.     theAE.dataHandle = theAEReply.dataHandle = target.dataHandle = NULL;
  92.  
  93.     fError = ::AECreateDesc(typeProcessSerialNumber, (Ptr)thePSN, sizeof(ProcessSerialNumber), &target);
  94.     VASSERT(fError == noErr, ("Problems with AECreateDesc() = %d", fError));
  95.  
  96.     if (fError != noErr)
  97.         goto KillApplicationFalse;
  98.  
  99.     fError = ::AECreateAppleEvent(kCoreEventClass, kAEQuitApplication, &target, kAutoGenerateReturnID, kAnyTransactionID, &theAE);
  100.     VASSERT(fError == noErr, ("Problems with AECreateAppleEvent() = %d", fError));
  101.  
  102.     if (fError != noErr)
  103.     {
  104.         ::AEDisposeDesc(&target);
  105.         goto KillApplicationFalse;
  106.     }
  107.  
  108.     fError = ::AESend(&theAE, &theAEReply, kAENoReply, kAENormalPriority, kNoTimeOut, NULL, NULL);
  109.     VASSERT(fError == noErr, ("Problems with AESend() = %d", fError));
  110.  
  111.     ::AEDisposeDesc(&target);
  112.     ::AEDisposeDesc(&theAE);
  113.  
  114.     if (fError != noErr)
  115.         goto KillApplicationFalse;
  116.     else
  117.         goto KillApplicationOK;
  118.  
  119.  
  120. KillApplicationFalse:return false;
  121. KillApplicationOK:return true;
  122. }
  123.  
  124.  
  125. #pragma segment Process
  126. short TProcess::GetNumProcesses()
  127. // Get the amount of currently running processes.
  128. {
  129.     ProcessSerialNumber aPSN;
  130.     short num = 0;
  131.  
  132.     aPSN.highLongOfPSN = 0;
  133.     aPSN.lowLongOfPSN = kNoProcess;
  134.  
  135.     while (GetNextProcess(&aPSN) == noErr)
  136.         num++;
  137.  
  138.     return num;
  139. }
  140.  
  141.  
  142. #pragma segment Process
  143. ProcessInfoRec TProcess::GetProcessInfoRec()
  144. // Return the full ProcInfoRec of specified process, NULL if we got into trouble.
  145. {
  146.     ProcessInfoRec theRec;
  147.  
  148.     theRec.processName = NULL;
  149.     theRec.processAppSpec = NULL;
  150.     theRec.processInfoLength = sizeof(theRec);
  151.  
  152.     fError = ::GetProcessInformation(&fProcessID, &theRec);
  153.     VASSERT(fError == noErr, ("Problems with GetProcessInformation() = %d", fError));
  154.  
  155.     return theRec;
  156. }
  157.  
  158.  
  159. #pragma segment Process
  160. unsigned long TProcess::GetProcessSize()
  161. // Return size of process.
  162. {
  163.     ProcessInfoRec temp = this->GetProcessInfoRec();
  164.     return temp.processSize;
  165. }
  166.  
  167.  
  168. #pragma segment Process
  169. unsigned long TProcess::GetFreeMem()
  170. // Return the amount of free memory available for the process
  171. {
  172.     ProcessInfoRec temp = this->GetProcessInfoRec();
  173.     return temp.processFreeMem;
  174. }
  175.  
  176.  
  177. #pragma segment Process
  178. unsigned long TProcess::GetLaunchDate()
  179. // Return in seconds the point when the application was launched.
  180. {
  181.     ProcessInfoRec temp = this->GetProcessInfoRec();
  182.     return temp.processLaunchDate;
  183. }
  184.  
  185.  
  186. #pragma segment Process
  187. Boolean TProcess::FindProcess(OSType signature)
  188. // Find process with the right signature, style 'MACS'.
  189. {
  190.     ProcessInfoRec theRec;
  191.     fProcessID.highLongOfPSN = 0;
  192.     fProcessID.lowLongOfPSN = kNoProcess;        // start from beginning
  193.  
  194.     theRec.processName = NULL;
  195.     theRec.processAppSpec = NULL;
  196.     theRec.processInfoLength = sizeof(theRec);
  197.  
  198.     while (::GetNextProcess(&fProcessID) == noErr)
  199.     {
  200.         if (::GetProcessInformation(&fProcessID, &theRec) == noErr)
  201.         {
  202.             if (theRec.processSignature == signature)
  203.                 goto FindProcessOK;                // we found it
  204.         }
  205.     }
  206.     goto FindProcessFalse;                        // we didn't find the process…
  207.  
  208. FindProcessFalse:return false;
  209. FindProcessOK:return true;
  210. }
  211.  
  212.  
  213. //    PUBLIC ACCESSORS AND MUTATORS            
  214.  
  215. #pragma segment Process                       
  216. ProcessSerialNumber TProcess::GetMyProcessID() const
  217. {
  218.     return fMyProcessID;
  219. }
  220.  
  221.  
  222. #pragma segment Process                       
  223. ProcessSerialNumber TProcess::GetProcessID() const
  224. {
  225.     return fProcessID;
  226. }
  227.  
  228.  
  229. // ITERATORS
  230.  
  231. #pragma segment Process                       
  232. void TProcess::Next()
  233. {
  234.     this->IProcess();
  235. }
  236.  
  237.  
  238. #pragma segment Process                       
  239. Boolean TProcess::Last()
  240. {
  241.     return fLast;
  242. }
  243.  
  244.  
  245. #pragma segment Process                       
  246. void TProcess::First()
  247. {
  248.     fProcessID = fFirstPSN;
  249. }
  250.  
  251.  
  252. // _________________________________________________________________________________________________________ //
  253.  
  254. /*    Change History (most recent last):
  255.   No        Init.    Date        Comment
  256.   1            khs        6/10/92        New file
  257.   2            khs        7/6/92        First decent working class
  258. */
  259.